package com.scqkzqtz.base.base;

import android.app.ActionBar;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import com.scqkzqtz.base.R;
import com.scqkzqtz.base.utils.ActivityUtil;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import me.bakumon.statuslayoutmanager.library.OnStatusChildClickListener;
import me.bakumon.statuslayoutmanager.library.StatusLayoutManager;

/**
 * Created by zsx on 2018/1/15.
 */

public abstract class BaseActivity<SV extends ViewDataBinding> extends FragmentActivity {
    protected SV binding;
    public StatusLayoutManager statusLayoutManager;
    private LayoutInflater inflater;
    private View emptyView, errorView, loadingView;
    private CompositeDisposable mCompositeDisposable;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityUtil.getInstance().addActivity(this);
        ActionBar actionBar = getActionBar();
        if (actionBar != null)
            actionBar.hide();
        binding = DataBindingUtil.setContentView(this, setLayoutId());
        init();

    }

    /**
     * 布局
     */
    public abstract int setLayoutId();

    /**
     * 初始化
     */
    public abstract void init();

    /**
     * 初始化布局状态
     * ----------------注意：这里不能传最外层view-----------------
     */
    public void initStatusLayoutManager(View view) {
        emptyView = inflate(R.layout.layout_empty);
        errorView = inflate(R.layout.layout_error);
        loadingView = inflate(R.layout.layout_loading);
        statusLayoutManager = new StatusLayoutManager.Builder(view)
                // 自定义布局
                .setLoadingLayout(loadingView)//加载中布局
                .setEmptyLayout(getEmptyLayout())//加载空布局
                .setErrorLayout(getErrorLayout())//加载失败布局
                .setEmptyClickViewID(getClickEmptyId())
                .setErrorClickViewID(getClickErrorId())
                .setDefaultLayoutsBackgroundColor(Color.WHITE)

                // 设置重试事件监听器
                .setOnStatusChildClickListener(new OnStatusChildClickListener() {
                    @Override
                    public void onEmptyChildClick(View view) {
                        //空状态布局
                        statusLayoutManager.showLoadingLayout();
                        onAgainRefresh();
                    }

                    @Override
                    public void onErrorChildClick(View view) {
                        //出错状态布局
                        statusLayoutManager.showLoadingLayout();
                        onAgainRefresh();
                    }

                    @Override
                    public void onCustomerChildClick(View view) {
                        //自定义状态布局返回
                    }
                })
                .build();

        ImageView img = loadingView.findViewById(R.id.im_loading);
        img.setImageResource(R.drawable.loading_frame);
        // 加载动画
        AnimationDrawable mAnimationDrawable = (AnimationDrawable) img.getDrawable();
        // 默认进入页面就开启动画
        if (!mAnimationDrawable.isRunning()) {
            mAnimationDrawable.start();
        }

        // 加载中
        statusLayoutManager.showLoadingLayout();
//        // 空数据
//        statusLayoutManager.showEmptyLayout();
//        // 加载失败
//        statusLayoutManager.showErrorLayout();
//        // 加载成功，显示原布局
//        statusLayoutManager.showSuccessLayout();
    }

    /**
     * 无数据布局
     */
    public View getEmptyLayout() {
        return emptyView;
    }

    /**
     * @return 返回点击无数据重新加载时的布局ID
     */
    public int getClickEmptyId() {
        return R.id.layout_root;
    }

    /**
     * 加载失败布局
     */
    public View getErrorLayout() {
        return errorView;
    }

    /**
     * @return 返回点击加载失败重新加载时的布局ID
     */
    public int getClickErrorId() {
        return R.id.layout_root;
    }

    /**
     * 重新加载
     */
    public void onAgainRefresh() {

    }

    public View inflate(@LayoutRes int resource) {
        if (inflater == null) {
            inflater = LayoutInflater.from(this);
        }
        return inflater.inflate(resource, null);
    }

    public void addDisposable(Disposable s) {
        if (this.mCompositeDisposable == null) {
            this.mCompositeDisposable = new CompositeDisposable();
        }
        this.mCompositeDisposable.add(s);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ActivityUtil.getInstance().removeActivity(this);
        if (this.mCompositeDisposable != null && mCompositeDisposable.size() > 0) {
            this.mCompositeDisposable.dispose();
        }
    }

    public void removeDisposable() {
        if (this.mCompositeDisposable != null && mCompositeDisposable.size() > 0) {
            this.mCompositeDisposable.dispose();
        }
    }

}
